#include "KeTogLabelIcon.h"

#include "../../uitools/vhc/constview4fun.h"
#include "../../uitools/vhc/constview4id.h"

#include "../../rwidgets/MacroFunOfView.h"
#include "../../uitools/IdRootMenu.h"
#include "../../uitools/TViewDraw.h"





KeTogLabelIcon::KeTogLabelIcon(QWidget *parent,uint iconCode,const QString label)
    : QWidget{parent}
    ,isWindowDeactivate(false)
    ,m_isMouseOver(false)
    ,isMouseDown(false)
    ,m_isChecked(false)
    ,m_codeIcon(iconCode)
    ,m_strLabel(label)
{
    QFont fontLabel;
    QString strLabel(label);

    fontLabel.setPointSize(TextSize_Tool);
    QFontMetrics fontMetrics(fontLabel);
    QRect rect = fontMetrics.boundingRect(strLabel);
    int   nLabelWidth = rect.width();
    int width_this = Width_Left_icon + nLabelWidth + 6;
    QSizePolicy sizePoFixed(QSizePolicy::Policy::Fixed, QSizePolicy::Policy::Fixed);
    sizePoFixed.setHorizontalStretch(0);
    sizePoFixed.setVerticalStretch(0);
    sizePoFixed.setHeightForWidth(false);


    setSizePolicy(sizePoFixed);
    setFixedSize(width_this,Height_CELL);


    setMouseTracking(true);
    installEventFilter(this);
}



bool KeTogLabelIcon::eventFilter(QObject *watched, QEvent *event)
{
    WindowDeactivate_EventFilter_Only()
}

void KeTogLabelIcon::setIconFont(const QFont &f)
{
    fontIcon = f;
    fontIcon.setPointSize(TextSize_IconFont);
}

void KeTogLabelIcon::leaveEvent(QEvent *event)  {
    m_isMouseOver = false;
    QWidget::leaveEvent(event);
    repaint();
}
void KeTogLabelIcon::enterEvent(QEnterEvent *event){
    m_isMouseOver = true;
    QWidget::enterEvent(event);
    repaint();
}
void KeTogLabelIcon::mousePressEvent(QMouseEvent *event)
{
    QWidget::mousePressEvent(event);

    Qt::MouseButton btn = event->button();
    if(Qt::MouseButton::LeftButton == btn){
        isMouseDown = true;
        ptDown = event->position();

        repaint();
    }
}
void KeTogLabelIcon::mouseReleaseEvent(QMouseEvent *event)
{
    QWidget::mouseReleaseEvent(event);


    bool _isMouseLeftDown = isMouseDown;
    isMouseDown=false;
    if(_isMouseLeftDown){
        Qt::MouseButton btn = event->button();
        if (Qt::MouseButton::LeftButton == btn)
        {
            QPointF pointUp = event->position();
            QPointF  ptUp = pointUp - ptDown;
            int x = ptUp.x(),y = ptUp.y();
            bool isClicked = x >-DisTorClicked && x < DisTorClicked && y >-DisTorClicked && y < DisTorClicked;
            if(isClicked)
            {
                int isCheckedFlag = m_isChecked ? 0:1;
                m_isChecked = !m_isChecked;

                Q_EMIT toggled(isCheckedFlag);
            }
        }
    }
    repaint();
}

void KeTogLabelIcon::paintEvent(QPaintEvent *event)
{
    QWidget::paintEvent(event);


    bool isTouchedDown = isMouseDown,isChecked = this->m_isChecked;
    bool isMouseOver = this->m_isMouseOver;
    bool isDeAct = false == isMouseOver && isWindowDeactivate;
    quint32 textColor = isChecked ?  Color_Font_ToolBar_checked:(isTouchedDown?Color_Font_Touched:Color_Font_ToolBar);
    int widthWindow ,heightWidow;
    int nLabelWidth,cx0,cy0;
    int ntLeft,ntTop;

    QSize rectWin = this->size();
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing |QPainter::VerticalSubpixelPositioning);


    widthWindow  = rectWin.width();
    heightWidow = rectWin.height();

    painter.fillRect(0,0,widthWindow ,heightWidow,Qt::transparent);

    if(isMouseOver && isWindowDeactivate)
    {

        QBrush brushBackDown(Qt::GlobalColor::white,Qt::SolidPattern);

        QBrush brushBorder(Qt::GlobalColor::lightGray,Qt::SolidPattern);
        QPen penBorder(brushBorder,1.0f, Qt::SolidLine);
        painter.setPen(penBorder);
        painter.setBrush(brushBackDown);
        painter.drawRect(0,0,widthWindow ,heightWidow);
    }else
    {
        if(isChecked && false == isDeAct
            && false == isMouseOver
            && false == isTouchedDown)
        {
            QBrush _brushTogBtn(QColor(QRgba64::fromArgb32(Checked_Background)),Qt::SolidPattern);

            painter.fillRect(0, 0, widthWindow ,heightWidow,_brushTogBtn);
        }

        if (  isMouseOver
            && false == isTouchedDown){

            quint32 nBackColor =isDeAct?0xffffffffU: (isChecked ?  Checked_Background_Over : Background_BtnOverR2);
            QBrush brushBackOver(QColor(QRgba64::fromArgb32(nBackColor)),Qt::SolidPattern);


            QBrush brushBorder(Qt::GlobalColor::lightGray,Qt::SolidPattern);
            QPen penBorder(brushBorder,1.0f, Qt::SolidLine);
            painter.setPen(penBorder);
            painter.setBrush(brushBackOver);
            painter.drawRect(0,0,widthWindow ,heightWidow);


        }
        if ( isTouchedDown)
        {
            quint32 nBackColor = isChecked ?  Checked_Background2_Touched:Background_TogBtnDown;
            QBrush brushBackDown(QColor(QRgba64::fromArgb32(nBackColor)),Qt::SolidPattern);

            QBrush brushBorder(Qt::GlobalColor::lightGray,Qt::SolidPattern);
            QPen penBorder(brushBorder,1.0f, Qt::SolidLine);
            painter.setPen(penBorder);
            painter.setBrush(brushBackDown);
            painter.drawRect(0,0,widthWindow ,heightWidow);

        }
    }





    if(isDeAct)  textColor = Color_Text_Deactivate;

    QBrush brushText_icon(QColor(QRgba64::fromArgb32(textColor)),Qt::SolidPattern);
    QPen penText_icon(brushText_icon,1, Qt::SolidLine);
    painter.setBrush(brushText_icon);
    painter.setPen(penText_icon);
    /*
    QString txt("<svg t=\"1737539119913\" class=\"icon\" viewBox=\"0 0 1024 1024\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" p-id=\"21590\" width=\"200\" height=\"200\"><path d=\"M1014.471089 954.589197l-84.875097-121.688393 9.71462-19.429239c67.491041-98.168787 64.423267-243.888081-6.646845-346.65853-1.533887-2.045183-2.556479-4.090366-3.579071-5.624253-75.160478-102.259153-201.961828-131.403012-305.754868-76.18307-11.759803 6.135549-23.519605 13.29369-34.256817 21.985718-4.090366 2.556479-7.669437 6.135549-11.759802 9.203324-24.030901 20.451831-44.994027 45.505323-61.355492 74.137886-6.646845 12.271098-12.782394 24.542197-17.895352 37.835887-34.256816 89.988055-25.564788 198.382758 32.211633 281.723968 12.271098 17.895352 26.58738 34.256816 42.948845 48.573097 81.807323 72.092703 197.360166 81.296027 288.882108 20.451831l89.476759 128.335238c6.135549 9.203324 15.850169 15.338873 26.076084 16.361464 10.225915 1.533887 20.963126-1.533887 29.143859-8.692028 8.180732-7.158141 13.804986-17.384056 15.338873-29.143859 1.533887-10.737211-1.533887-21.985718-7.669437-31.189041z m-164.125941-144.185407c-66.46845 56.75383-157.990392 59.821605-228.037912 8.180733-12.782394-9.203324-24.542197-20.451831-34.256816-32.211634-4.090366-5.112958-7.669437-9.71462-11.248507-14.827577-49.595689-70.558816-53.17476-163.614645-15.850169-237.241236 12.782394-26.076084 30.16645-48.061802 51.640873-65.957154 79.250844-68.002337 192.758504-58.287717 261.272136 23.00831 10.737211 12.271098 19.940535 26.076084 27.098676 40.903661 47.550506 92.033238 28.121267 211.676447-50.618281 278.144897zM712.806587 287.859517c82.318618 0 159.012983 27.098676 222.924954 72.603999-22.497014-99.702675-43.46014-188.156842-54.708647-224.970138-28.632563-97.146196-86.408985-132.936899-168.216307-132.936899h-116.064139l44.482732 292.461178c23.519605-5.112958 47.550506-7.158141 71.581407-7.15814zM568.109885 245.421968H471.986281l-7.669437 173.840561c30.677746-34.768112 67.491041-63.400675 107.372111-85.386393l-3.57907-88.454168z m-10.225915-241.842898H482.212196l-7.158141 161.058167h89.47676L557.88397 3.57907zM18.466936 745.980524C7.218429 784.327706 1.08288 818.073227 0.060288 847.217086v14.827577C2.105471 929.535704 36.873583 966.348999 104.87592 966.348999h176.908335l55.219943-312.913009H43.520428c-9.203324 36.301999-18.406648 68.513633-25.053492 92.544534zM179.525102 119.643209c-8.692028 27.098676-20.451831 72.092703-33.74552 125.267463h262.806024L451.023154 1.533887H295.077945c-62.378084 5.624253-81.807323 10.737211-115.552843 118.109322z m-114.018956 446.872501h286.32563l40.903661-233.662166H123.793864c-19.429239 75.671773-39.369774 158.501688-58.287718 233.662166z\" p-id=\"21591\"></path></svg>");
    QSvgRenderer render;
    QByteArray contents= txt.toLocal8Bit();
    render.load(contents);
    QRectF bounds(4,6,22,22);
    render.render(&painter,bounds);
*/

    {


        int left = 4,childWidth=32,childHeight = heightWidow;
        int sizeTextIcon = 14;



        int cx0, cy0,ntLeft,ntTop;
        uint codeText = m_codeIcon;
        QChar chTxt(codeText);
        QString szLabel(chTxt);
        QFont &font_t = fontIcon;

        font_t.setPointSize(sizeTextIcon);


        cx0 = left + (childWidth >> 1);
        cy0 = (childHeight >> 1);
        ntLeft = cx0 - 1000;
        ntTop = cy0 - 500;


        QRect rect4Text(ntLeft, ntTop, 2000, 1000);

        painter.setFont(font_t);


        painter.drawText(rect4Text,Qt::AlignCenter,szLabel);

    }

    QString strLabel(m_strLabel);
    QFont fontLabel;
    fontLabel.setPointSize(TextSize_Tool);


    QFontMetrics fontMetrics(fontLabel);
    QRect rect = fontMetrics.boundingRect(strLabel);
    nLabelWidth = rect.width();


    cx0 = Width_Left_icon + (nLabelWidth >> 1);
    cy0 =  (heightWidow >> 1);
    ntLeft = cx0 - 1000;
    ntTop = cy0 - 500;


    QRect rect4Text_icon(ntLeft, ntTop, 2000, 1000);

    painter.setFont(fontLabel);


    painter.setBrush(Qt::BrushStyle::NoBrush);
    painter.setPen(penText_icon);
    painter.drawText(rect4Text_icon,Qt::AlignCenter,strLabel);

    if(isChecked || isTouchedDown)
    {
        int boolFlag = isChecked ? BoolFlagChecked : 0;


        if(isDeAct)boolFlag |= BoolFlagDeactivate;
        if(isMouseOver)boolFlag |= BoolFlagMouseOver;
        if(isTouchedDown)boolFlag |= BoolFlagTouched;
        kepen::core::TViewDraw::DrawCheckedTriFlag(&painter,0, 0, widthWindow ,heightWidow,boolFlag);
    }


}
